<template>
  <div class="permission">
    <el-card>
      <div style="text-align: right">
        <el-button type="primary" @click="add(1, '0')">添加权限页面</el-button>
      </div>
      <el-table :data="list" row-key="id">
        <el-table-column label="菜单名称" prop="name" />
        <el-table-column label="权限标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <div style="width: 500px">
              <el-button
                v-if="row.type == 1"
                type="text"
                @click="add(1, row.id)"
              >添加权限页面</el-button>

              <el-button
                v-if="row.type == 1"
                type="text"
                @click="add(2, row.id)"
              >添加权限按钮</el-button>
              <el-button type="text" @click="edit(row)">修改权限点</el-button>
              <el-button type="text" @click="del(row)">删除权限点</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加弹框 -->
    <Add ref="add" v-model="show" @getDate="getDate" />
  </div>
</template>

<script>
import { sysPermissionAPI, sysPermissionDelAPI } from '@/api'
export default {
  components: {
    Add: () => import('./conponents/add.vue')
  },
  data() {
    return {
      list: [{}],
      show: false
    }
  },
  created() {
    this.getDate()
  },
  methods: {
    async getDate() {
      const res = await sysPermissionAPI()
      this.list = this.changeDate(res.data, '0')
    },
    changeDate(arr, pid) {
      return arr.filter((item) => {
        if (item.pid == pid) {
          item.children = this.changeDate(arr, item.id)
          return true
        }
      })
    },
    // 新增弹框
    add(type, pid) {
      this.show = true
      this.$refs.add.form.type = type
      this.$refs.add.form.pid = pid
      this.$refs.add.mode = 'add'
    },
    // 删除权限点
    del(row) {
      this.$confirm(`您确定要删除${row.name}吗`, '提示')
        .then(async() => {
          const res = await sysPermissionDelAPI(row.id)
          this.getDate()
        })
        .catch(() => {})
    },
    // 编辑权限点
    edit(row) {
      this.show = true
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      this.$refs.add.mode = 'edit'
    }
  }
}
</script>

<style lang="scss" scopen>
.permission {
  padding: 15px;
}
</style>
